package com.ruoyi.miniapp.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.map.SoMap;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.miniapp.domain.TMiniSportRecord;
import com.ruoyi.miniapp.service.ITMiniSportRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 运动记录Controller
 * 
 * @author ruoyi
 * @date 2025-07-17
 */
@RestController
@RequestMapping("/miniapp/record")
public class TMiniSportRecordController extends BaseController
{
    @Autowired
    private ITMiniSportRecordService tMiniSportRecordService;

//    /**
//     * 查询运动记录列表
//     */
//    @PreAuthorize("@ss.hasPermi('miniapp:record:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(TMiniSportRecord tMiniSportRecord)
//    {
//        startPage();
//        List<TMiniSportRecord> list = tMiniSportRecordService.selectTMiniSportRecordList(tMiniSportRecord);
//        return getDataTable(list);
//    }

    /**
     * 查询运动记录列表
     */
    @PreAuthorize("@ss.hasPermi('miniapp:record:list')")
    @GetMapping("/list")
    public AjaxResult list()
    {
        SoMap so = getRequestSoMap();
        PageInfo<TMiniSportRecord> list = tMiniSportRecordService.getSportHistoryList(so);
        return AjaxResult.success(list);
    }

    /**
     * 查询今日运动记录
     */
    @PreAuthorize("@ss.hasPermi('miniapp:record:list')")
    @GetMapping("/sportRecordList")
    public AjaxResult sportRecordList()
    {
        SoMap so = getRequestSoMap();
        List<TMiniSportRecord> result = tMiniSportRecordService.getTodaySportHistory(so);
        return AjaxResult.success(result);
    }

    /**
     * 统计用户运动量
     * @return
     */
    @GetMapping("/userSport")
    public R userSport() {
        SoMap so = getRequestSoMap();
        so.put("recordTime",DateUtils.getToday());
        List sportList = tMiniSportRecordService.anlysisSportType(so);
        return R.ok(sportList, "操作成功");
    }

    /**
     * 导出运动记录列表
     */
    @PreAuthorize("@ss.hasPermi('miniapp:record:export')")
    @Log(title = "运动记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TMiniSportRecord tMiniSportRecord)
    {
        List<TMiniSportRecord> list = tMiniSportRecordService.selectTMiniSportRecordList(tMiniSportRecord);
        ExcelUtil<TMiniSportRecord> util = new ExcelUtil<TMiniSportRecord>(TMiniSportRecord.class);
        util.exportExcel(response, list, "运动记录数据");
    }

    /**
     * 获取运动记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('miniapp:record:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(tMiniSportRecordService.selectTMiniSportRecordByRecordId(recordId));
    }

    /**
     * 新增运动记录
     */
    @PreAuthorize("@ss.hasPermi('miniapp:record:add')")
    @Log(title = "运动记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TMiniSportRecord tMiniSportRecord)
    {
        return toAjax(tMiniSportRecordService.insertTMiniSportRecord(tMiniSportRecord));
    }

    /**
     * 修改运动记录
     */
    @PreAuthorize("@ss.hasPermi('miniapp:record:edit')")
    @Log(title = "运动记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TMiniSportRecord tMiniSportRecord)
    {
        return toAjax(tMiniSportRecordService.updateTMiniSportRecord(tMiniSportRecord));
    }

    /**
     * 删除运动记录
     */
    @PreAuthorize("@ss.hasPermi('miniapp:record:remove')")
    @Log(title = "运动记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(tMiniSportRecordService.deleteTMiniSportRecordByRecordIds(recordIds));
    }


}
